package com.sara.web;

import com.sara.entity.Admin;
import com.sara.exception.ServiceException;
import com.sara.service.AdminService;
import com.sara.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends BaseServlet {

    private Logger logger = LoggerFactory.getLogger(LoginServlet.class);

    private static final long serialVersionUID = 1L;


    @Override
    protected void  doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        String adminName = null;
        if(cookies != null){
            for(Cookie cookie : cookies){
                if("adminName".equals(cookie.getName())){
                   adminName = cookie.getValue();
                   break;
                }
            }
        }
        req.setAttribute("adminName",adminName);
        System.out.println("从登录这过。。。");
        forword("Login",req,resp);

    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AdminService adminService = new AdminService();

        String adminName = req.getParameter("adminName");
        String password = req.getParameter("password");
        String remember = req.getParameter("remember");
        System.out.println("adminName :" + adminName);
        System.out.println("password : " + password);
        System.out.println("remember : " + remember);

        try{
            Admin admin = adminService.login(adminName,password);
            logger.info("{}登录了管理系统 ",adminName);
            HttpSession session = req.getSession();
            session.setAttribute("admin",admin);

            if(remember != null){
                Cookie cookie = new Cookie("adminName",adminName);
                cookie.setDomain("127.0.0.1");
                cookie.setPath("/");
                cookie.setMaxAge(60*60*24*7);

                resp.addCookie(cookie);
            }else {
                Cookie[] cookies = req.getCookies();
                if(cookies != null){
                    for(Cookie cookie : cookies){
                        if("adminName".equals(cookie.getName())){
                            cookie.setDomain("127.0.0.1");
                            cookie.setPath("/");
                            cookie.setMaxAge(0);

                            resp.addCookie(cookie);
                            break;
                        }
                    }
                }
            }
            JsonResult res = JsonResult.success(admin);
            sendJson(res,resp);
        }catch (ServiceException e){
            JsonResult res = JsonResult.error(e.getMessage());
            System.out.println("错误被捕捉，，，，");
            sendJson(res,resp);
        }
    }


}
